package com.ruoyi.business.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.business.domain.Rack;
import com.ruoyi.business.service.IRackService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 台架Controller
 * 
 * @author qiancheng
 * @date 2025-06-13
 */
@Api(tags = "台架管理")
@RestController
@RequestMapping("/business/rack")
public class RackController extends BaseController
{
    @Autowired
    private IRackService rackService;

    /**
     * 实验平台管理->>台架维护管理->>查询台架列表
     */
    @ApiOperation("查询台架列表")
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(@ApiParam("台架查询条件") Rack rack)
    {
        List<Rack> list = rackService.selectRackList(rack);
        return getDataTable(list);
    }

    /**
     * 导出台架列表
     */
    @ApiOperation("导出台架列表")
    @PreAuthorize("@ss.hasPermi('business:rack:export')")
    @Log(title = "台架", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @ApiParam("台架查询条件") Rack rack)
    {
        List<Rack> list = rackService.selectRackList(rack);
        ExcelUtil<Rack> util = new ExcelUtil<Rack>(Rack.class);
        util.exportExcel(response, list, "台架数据");
    }

    /**
     * 获取台架详细信息
     */
    @ApiOperation("获取台架详细信息")
    @PreAuthorize("@ss.hasPermi('business:rack:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam("台架ID") @PathVariable("id") Long id)
    {
        return success(rackService.selectRackById(id));
    }

    /**
     * 实验平台管理->>台架维护管理->>新增台架
     */
    @ApiOperation("新增台架")
    @RepeatSubmit
    @Anonymous
    @PostMapping
    public AjaxResult add(@ApiParam("文件") @RequestParam(value = "file", required = false) MultipartFile file,
                          @ApiParam("台架信息") @RequestParam("rack") String rack)
    {
        try {
            // 将JSON字符串转换为RackEquipment对象
            Rack ra = JSON.parseObject(rack, Rack.class);
            return toAjax(rackService.insertRack(file,ra));
        } catch (Exception e) {
            return error("新增台架失败：" + e.getMessage());
        }
    }

    /**
     * 实验平台管理->>台架维护管理->>修改台架
     */
    @ApiOperation("修改台架")
    @Anonymous
    @PutMapping
    public AjaxResult edit(@ApiParam("台架信息") @RequestBody Rack rack)
    {
        return toAjax(rackService.updateRack(rack));
    }

    /**
     * 实验平台管理->>台架维护管理->>删除台架
     */
    @ApiOperation("删除台架")
    @Anonymous
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("台架ID数组") @PathVariable Long[] ids)
    {
        return toAjax(rackService.deleteRackByIds(ids));
    }
}
